package com.example.yls.howold;

import android.graphics.Bitmap;
import android.util.Base64;
import android.util.Log;

import com.megvii.cloud.http.CommonOperate;
import com.megvii.cloud.http.Response;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;

import javax.security.auth.login.LoginException;

/**
 * ClassName:
 * Description:
 */

public class FaceppDetect {

    public interface CallBack
    {
        void success(JSONObject result);
//        void error(FaceppParseException exception);
    }
    public static void detect(final Bitmap bm,final CallBack callBack)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    CommonOperate commonOperate = new CommonOperate(Constant.KEY, Constant.SECRET, false);
                    //把图片转换成二进制
                    Bitmap bmSmall = Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight());
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bmSmall.compress(Bitmap.CompressFormat.JPEG,100,stream);
                    byte[] arrays = stream.toByteArray();
                    String base64 = Base64.encodeToString(arrays, Base64.NO_WRAP);
                    Response response= commonOperate.detectBase64(base64, 0, "gender,age");
                    String res = new String(response.getContent());
                    JSONObject json = new JSONObject(res);
                    Log.e("JSONObject", json.toString());
                    if(callBack!=null){
                        callBack.success(json);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
}
